//
//  ClassifyViewController.m
//  Luck
//
//  Created by 纳里健康 on 2018/5/22.
//  Copyright © 2018年 songK. All rights reserved.
//

#import "LGClassifyViewController.h"
#import "LGClassCollectionViewController.h"
#import "LGClassTabAndColViewController.h"


@interface LGClassifyViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *titleView;

//指示器View
@property (nonatomic, strong) UIView *indicator;

@property (nonatomic, strong) UIButton *selectBtn;

@property (nonatomic, strong) UIScrollView *contentView;
@end

@implementation LGClassifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupChildVc];
    
    [self setupNavTitle];
    
    [self setupScrollView];
    
}

/**
 * 初始化子控制器
 */
- (void)setupChildVc
{
    LGClassCollectionViewController *all = [[LGClassCollectionViewController alloc] init];
    all.title = @"品牌";
    [self addChildViewController:all];
    
    LGClassTabAndColViewController *video = [[LGClassTabAndColViewController alloc] init];
    video.title = @"款式";
    [self addChildViewController:video];
}


- (void)setupNavTitle
{
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    
    self.navigationItem.titleView = self.titleView;
    
    //button 下面的下划线
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = UIColorFromRGB(0xAD9B6A);
    indicatorView.height = 2.0f;
    indicatorView.y = 40 - 2;
    indicatorView.frame = CGRectMake(0, 38, 15, 2);
    self.indicator = indicatorView;
    
    //创建按钮
    NSArray *titleArray = self.childViewControllers;
    
    CGFloat btnWidth = 100;
    
    CGFloat btnHeight = 40;
    
    for (int i = 0 ; i < titleArray.count ; i ++ ) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * btnWidth, 0, btnWidth, btnHeight);
        button.tag = i;
        
        UITableViewController *vc = titleArray[i];
        
        [button setTitle:vc.title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:18];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(titleSelect:) forControlEvents:UIControlEventTouchUpInside];
        
        //进来就选中第一个
        if(i == 0)
        {
            button.enabled = NO;
            _selectBtn = button;
            
            [button layoutIfNeeded];

            self.indicator.centerX = button.centerX;
        }
        
        [self.titleView addSubview:button];
    }
    
    [self.titleView addSubview:indicatorView];
}

#pragma mark -- buttonAction
- (void)titleSelect:(UIButton *)btn
{
    //点击按钮后 指示器移动
    [UIView animateWithDuration:0.15 animations:^{
        
        self.indicator.centerX = btn.centerX;
        
    }];
    
    //设置选中按钮 实现点击的时候只有一个按钮为红色
    self.selectBtn.enabled = YES;
    btn.enabled = NO;
    self.selectBtn = btn;
    
    //点击按钮时候滚动scrollView
    CGPoint offset = self.contentView.contentOffset;
    offset.x = btn.tag * self.contentView.width;
    
    [self.contentView setContentOffset:offset animated:YES];
}


#pragma mark -- 初始化滑动视图
- (void)setupScrollView
{
    UIScrollView *contentView = [[UIScrollView alloc] init];
    
    contentView.pagingEnabled = YES;
    contentView.delegate = self;
    
    self.contentView = contentView;
    
    contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATOR_HEIGHT - TABBAR_HEIGHT );
    
    contentView.contentSize = CGSizeMake(self.childViewControllers.count * contentView.width, 0);
    
    //关闭系统设定的inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view insertSubview:contentView atIndex:0];
    
    //进去就加载第一个控制器
    [self scrollViewDidEndScrollingAnimation:contentView];
    
    
}

#pragma mark -- scrollView代理
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //滑动结束以后要知道记在哪一个控制器 根据偏移量/宽度 获取偏移了多少宽度
    NSInteger index = scrollView.contentOffset.x/scrollView.width;
    
    //从childViewControllers的数组中取出当前的控制器
    UIViewController *currentController = self.childViewControllers[index];
    
    currentController.view.frame = CGRectMake(scrollView.contentOffset.x, 0, scrollView.width, scrollView.height);
    
    [scrollView addSubview:currentController.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    
    UIButton *btn = self.titleView.subviews[index];
    
    [self titleSelect:btn];
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
